import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect/index')
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/auth-redirect',
    component: () => import('@/views/login/auth-redirect'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/error-page/404'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('@/views/error-page/401'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        component: () => import('@/views/dashboard/index'),
        name: 'Dashboard',
        meta: { title: 'Dashboard', icon: 'dashboard', affix: true }
      }
    ]
  },
  {
    path: '/profile',
    component: Layout,
    redirect: '/profile/index',
    hidden: true,
    children: [
      {
        path: 'index',
        component: () => import('@/views/profile/index'),
        name: 'Profile',
        meta: { title: 'Profile', icon: 'user', noCache: true }
      }
    ]
  }
]

/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 */
export const asyncRoutes = [
  /*{
    path: '/sys',
    component: Layout,
    redirect: '/sys/user',
    alwaysShow: true, // will always show the root menu
    name: 'Sys',
    meta: {
      title: '系统设置',
      icon: 'lock',
      roles: ['admin', 'editor'] // you can set roles in root nav
    },
    children: [
      {
        path: 'login',
        component: () => import('@/views/sys/login'),
        name: 'Login',
        meta: {
          title: '登录'
          // if do not set roles, means: this page does not require permission
        }
      },
      {
        path: 'es',
        component: () => import('@/views/sys/es'),
        name: 'Es',
        meta: {
          title: 'es全量同步'
          // if do not set roles, means: this page does not require permission
        }
      },
      {
        path: 'user',
        component: () => import('@/views/sys/user'),
        name: 'User',
        meta: {
          title: '人员管理'
          // if do not set roles, means: this page does not require permission
        }
      },
      {
        path: 'dept',
        component: () => import('@/views/sys/dept'),
        name: 'Dept',
        meta: {
          title: '部门管理'
          // if do not set roles, means: this page does not require permission
        }
      },
      {
        path: 'role',
        component: () => import('@/views/sys/role'),
        name: 'Role',
        meta: {
          title: '角色管理'
          // if do not set roles, means: this page does not require permission
        }
      },
      {
        path: 'business',
        component: () => import('@/views/sys/business'),
        name: 'Business',
        meta: {
          title: '商户管理'
          // if do not set roles, means: this page does not require permission
        }
      },
      {
        path: 'menu',
        component: () => import('@/views/sys/menu'),
        name: 'Menu',
        meta: {
          title: '菜单管理'
          // if do not set roles, means: this page does not require permission
        }
      },
      {
        path: 'sql',
        component: () => import('@/views/sys/sql'),
        name: 'Sql',
        meta: {
          title: 'SQL监控'
          // if do not set roles, means: this page does not require permission
        }
      },
      {
        path: 'log',
        component: () => import('@/views/sys/log'),
        name: 'Log',
        meta: {
          title: '系统日志'
          // if do not set roles, means: this page does not require permission
        }
      },
      {
        path: 'interface',
        component: () => import('@/views/sys/interface'),
        name: 'Interface',
        meta: {
          title: '接口管理'
          // if do not set roles, means: this page does not require permission
        }
      }
    ]
  }*/
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
